//
//  KKCustomerSelectedView.m
//  Butler
//
//  Created by liaozhenming on 16/6/20.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKCustomerSelectedView.h"
#import "CustomerCollectionViewCell.h"

@interface KKCustomerSelectedView()

@property (nonatomic, strong) NSArray *customerItems;
@property (nonatomic, strong) KKCustomer *customerItem;

@end

@implementation KKCustomerSelectedView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        UIView *contentView = [self contentView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(contentView.bounds), 44)];
        titleLabel.backgroundColor = [UIColor kk_colorWithHexString: @"71d47c"];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = @"选择客户";
        [contentView addSubview: titleLabel];
        
        UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(0, 44, CGRectGetWidth(contentView.bounds), 40)];
        searchBar.placeholder = @"姓名/手机号/微信号/会员号";
        [contentView addSubview: searchBar];
        
        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
        flowLayout.itemSize = CGSizeMake(160, 120);
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake(5, 84, CGRectGetWidth(contentView.bounds) - 10, CGRectGetHeight(contentView.bounds) - 134) collectionViewLayout: flowLayout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [contentView addSubview: collectionView];
        _customerCollectionView = collectionView;
        
        UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeSystem];
        cancelButton.frame = CGRectMake(30, CGRectGetHeight(contentView.bounds) - 50, 120, 34);
        cancelButton.backgroundColor = [UIColor kk_colorWithHexString: @"71d47c"];
        [cancelButton setTitle: @"取消" forState: UIControlStateNormal];
        [cancelButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
        [cancelButton addTarget: self action: @selector(action_cancel:) forControlEvents: UIControlEventTouchUpInside];
        [cancelButton kk_cornerRadius: 3.0];
        [contentView addSubview: cancelButton];
        
        UIButton *sureButton = [UIButton buttonWithType: UIButtonTypeSystem];
        sureButton.frame = CGRectMake(CGRectGetWidth(contentView.bounds) - 150, CGRectGetHeight(contentView.bounds) - 50, 120, 34);
        sureButton.backgroundColor = [UIColor kk_colorWithHexString: @"71d47c"];
        [sureButton setTitle: @"确定" forState: UIControlStateNormal];
        [sureButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
        [sureButton addTarget: self action: @selector(action_save:) forControlEvents: UIControlEventTouchUpInside];
        [sureButton kk_cornerRadius: 3.0];
        [contentView addSubview: sureButton];
        
        _customerItems = [[KKButlerDataManager kk_manager] arr_customerItems];
    }
    return self;
}

#pragma mark - Action methods

- (void)action_cancel:(UIButton*)item {
    
    [self kk_close];
}

- (void)action_save:(UIButton*)item {
    
    if (self.selectedResult) {
        self.selectedResult(_customerItem);
    }
    [self kk_close];
}

#pragma mark - UICollectionView methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _customerItems.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomerCollectionViewCell *cell = [CustomerCollectionViewCell kk_cellWithCollectionView: collectionView indexPath: indexPath];
    KKCustomer *item = _customerItems[indexPath.row];
    [cell setCustomerItem: item];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(5, 10, 5, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    _customerItem = _customerItems[indexPath.row];
}

@end
